'''
Created on 28 sept. 2010
@author: Marc CARRE
@contact: carre.marc@gmail.com
@license: GNU GPLv3
@version: 1.0
'''

import unittest
from SpacePartition import SpacePartition

class SpacePartitionTest(unittest.TestCase):

    def testIsInQuadraticSpace(self):
        p = self.createQuadraticSpace()
        point = [0, 1]
        self.assertTrue(p.isInSpace(point))
        
    def testIsNotInQuadraticSpace(self):
        p = self.createQuadraticSpace()
        point = [0, 0]
        self.assertFalse(p.isInSpace(point))
        
    def createQuadraticSpace(self):
        label = "A"
        params = ["x", "y"]
        limits = [[-2,2], [0,4.1]]
        space = "y>x**2"
        return SpacePartition(label, params, limits, space)
    
    def testIsInSinusoidalSpace(self):
        p = self.createSinusoidalSpace()
        point = [0, 1]
        self.assertTrue(p.isInSpace(point))
        
    def testIsNotInSinusoidalSpace(self):
        p = self.createSinusoidalSpace()
        point = [0, 0]
        self.assertFalse(p.isInSpace(point))
    
    def createSinusoidalSpace(self):
        label = "A"
        params = ["x", "y"]
        limits = [[-2,2], [0,4.1]]
        space = "y>sin(x)"
        return SpacePartition(label, params, limits, space)
    
    def testIncorrectCreation01(self):
        label = 3 # Should be a string
        params = ["x", "y"]
        limits = [[-2,2], [0,4]]
        space = "y>x**2"
        with self.assertRaises(ValueError):
            SpacePartition(label, params, limits, space)
    
    def testIncorrectCreation02(self):
        label = "A"
        params = "x" # Should be a list
        limits = [[-2,2], [0,4]]
        space = "y>x**2"
        with self.assertRaises(ValueError):
            SpacePartition(label, params, limits, space)

    def testIncorrectCreation03(self):
        label = "A"
        params = [0,1] # Should be a list of string
        limits = [[-2,2], [0,4]]
        space = "y>x**2"
        with self.assertRaises(ValueError):
            SpacePartition(label, params, limits, space)
            
    def testIncorrectCreation04(self):
        label = "A"
        params = ["x", "y"]
        limits = 4 # Should be a list
        space = "y>x**2"
        with self.assertRaises(ValueError):
            SpacePartition(label, params, limits, space)

    def testIncorrectCreation05(self):
        label = "A"
        params = ["x", "y"]
        limits = [0,1] # Should be a list of list of numerics
        space = "y>x**2"
        with self.assertRaises(ValueError):
            SpacePartition(label, params, limits, space)

    def testIncorrectCreation06(self):
        label = "A"
        params = ["x", "y"]
        limits = [[3,"B"], ["A","B"]] # Should be a list of list of numerics
        space = "y>x**2"
        with self.assertRaises(ValueError):
            SpacePartition(label, params, limits, space)

    def testIrrelevantCreation01(self):
        label = "A"
        params = ["x", "y"]
        limits = [[3,0]] # Should have two sets of min/max.
        space = "y>x**2"
        with self.assertRaises(ValueError):
            SpacePartition(label, params, limits, space)

    def testIrrelevantCreation02(self):
        label = "A"
        params = ["x", "y"]
        limits = [[3,0], [3]] # Should have two numerics in each set.
        space = "y>x**2"
        with self.assertRaises(ValueError):
            SpacePartition(label, params, limits, space)
            
    def testIrrelevantCreation03(self):
        label = "A"
        params = ["x", "y"]
        limits = [[5,0], [3,4]] # The minimum should be less than the maximum.
        space = "y>x**2"
        with self.assertRaises(ValueError):
            SpacePartition(label, params, limits, space)

    def testWarningMissingParam(self):
        label = "A"
        params = ["x", "y"]
        limits = [[0,5], [3,4]]
        space = "y>2"  # x is not used in the parametrization of the space.
        with self.assertRaises(Warning):
            SpacePartition(label, params, limits, space)
    
if __name__ == "__main__":
    unittest.main()